#include "ConfigManager.h"
#include "File.h"
#include "DX11ErrorReport.h"

#include <time.h>

ConfigManager::ConfigManager()
{
	m_seed = -1;
	TheEventPoller::Instance()->AddHandler(this);

	m_functionKeys["F1"] = 0x70;
	m_functionKeys["F2"] = 0x71;
	m_functionKeys["F3"] = 0x72;
	m_functionKeys["F4"] = 0x73;
	m_functionKeys["F5"] = 0x74;
	m_functionKeys["F6"] = 0x75;
	m_functionKeys["F7"] = 0x76;
	m_functionKeys["F8"] = 0x77;
	m_functionKeys["F9"] = 0x78;
	m_functionKeys["F10"] = 0x79;
	m_functionKeys["F11"] = 0x7A;
	m_functionKeys["F12"] = 0x7B;
}

ConfigManager::~ConfigManager()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void ConfigManager::DrawInfo()
{
	for (int i = 0; i != m_displayInfos.size(); i++)
	{
		if (m_displayInfos[i].type == "maze" && m_displayInfos[i].drawing)
		{
			// draw time to generate
			m_timeToGenerateMazeText.Draw();
		}
	}
	
	m_seedText.Draw();
}

bool ConfigManager::Load(const std::string& filename)
{
	File* f = new File();
	if (!f->OpenRead(filename))
	{
		DX11ErrorMsg(L"Failed to locate config file", L"ConfigManager::Load()", MB_OK);
		delete f;
		return false;
	}

	std::string lineContents;
	while (f->GetString(&lineContents))
	{
		Strings s = Split(lineContents, ':');

		if (s[0] == "seed")
		{
			if (s[1] == "rand")
			{
				m_seed = (int)time(NULL);
				if (!AppendToSeedFile(ToString(m_seed)))
				{
					DX11ErrorMsg(L"Failed to write to seed file", L"ConfigManager::Load()", MB_OK);
					delete f;
					return false;
				}
			}
			else
			{
				m_seed = ToInt(s[1]);
			}

			m_seedText.Update("Seed: " + ToString(m_seed));
			m_seedText.Translate(D3DXVECTOR3(0.0f, -0.9f, 0.0f));
		}
	}
	
	delete f;
	return true;
}

bool ConfigManager::LoadInfoSettings(const std::string& filename)
{	
	File* f = new File();
	if (!f->OpenRead(filename))
	{
		DX11ErrorMsg(L"Failed to locate config file", L"ConfigManager::Load()", MB_OK);
		delete f;
		return false;
	}

	std::string lineContents;
	std::string currentType = "";
	while (f->GetString(&lineContents))
	{
		Strings s = Split(lineContents, ':');

		if (s[0] == "type")
		{
			currentType = s[1];
			m_displayInfos.push_back(InfoDisplay());
			m_displayInfos.back().type = s[1];
			m_displayInfos.back().drawing = false;
			//m_displayInfos[currentType] = false;
		}
		else if (s[0] == "button")
		{
			m_displayInfos.back().button = s[1];
			//m_displayButtons[currentType] = s[1];
		}
	}

	return true;
}

bool ConfigManager::AppendToSeedFile(const std::string& text)
{
	ofstream seedFile;
	if (seedFile.is_open())
	{
		DX11ErrorMsg(L"File is open", L"ConfigManager::AppendToSeedFile()", MB_OK);
		return false;
	}
	seedFile.open("../../Config/seeds.log", ios::app);
	if (!seedFile.is_open())
	{
		DX11ErrorMsg(L"Failed to open file", L"ConfigManager::AppendToSeedFile()", MB_OK);
		return false;
	}

	std::string contents;
		
	contents += text + "\n";
	seedFile << contents;

	seedFile.close();

	return true;
}

void ConfigManager::GetMazeGenTime(const float time)
{
	m_timeToGenerateMaze = time;
	m_timeToGenerateMazeText.Translate(D3DXVECTOR3(0.0f, -0.8f, 0.0f));
	m_timeToGenerateMazeText.Update("Time to Generate: " + ToString(m_timeToGenerateMaze));
}

int ConfigManager::GetSeed() const
{
	return m_seed;
}

void ConfigManager::OnKeyDownEvent(const WPARAM& event)
{
	for (int i = 0; i != m_displayInfos.size(); i++)
	{
		if (event == m_functionKeys[m_displayInfos[i].button])
		{
			if (m_displayInfos[i].drawing)
			{
				m_displayInfos[i].drawing = false;
			}
			else
			{
				m_displayInfos[i].drawing = true;
			}				
		}
	}
}

void ConfigManager::OnKeyUpEvent(const WPARAM& event)
{

}